package cc.zy.base.hr.service.impl;

import cc.zy.base.common.entity.FebsConstant;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.common.utils.SortUtil;
import cc.zy.base.hr.entity.Staff;
import cc.zy.base.hr.entity.StaffLabel;
import cc.zy.base.hr.mapper.StaffMapper;
import cc.zy.base.hr.service.IStaffService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author ljy
 * @time 2022/03/09 10:28:06
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements IStaffService {

    @Autowired
    private StaffMapper staffMapper;

    @Override
    public IPage<Staff> getStaffs(QueryRequest request, Staff staff) {
        Page<StaffLabel> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countStaffDetail(staff));
        //SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        return baseMapper.getStaffDetailPage(page, staff);
    }

    @Override
    public Integer getConstantId(String constant) {
        return staffMapper.getConstantId(constant);
    }

    @Override
    public boolean staffExist(Staff staff) {
        Staff staff1 = staffMapper.staffExist(staff);
        boolean flag = false;
        if (staff1 != null) {
            flag = true;
        }
        return flag;
    }

    @Override
    public void updateStaffByUserId(Staff staff) {
        staff.setLastModifyDate(new Date());
        staffMapper.updateStaffByUserId(staff);
    }

    @Override
    public Date getUpdateTime() {
        return staffMapper.getUpdateTime();
    }

    @Override
    public List<String> getAllStaffUserIdList() {
        return staffMapper.getAllStaffUserIdList();
    }

    @Override
    public void deleteStaffByUserId(String userId) {
        staffMapper.deleteStaffByUserId(userId);
    }

    // 二组通用选择员工
    /**
     * 方法描述：方法描述：分页
     *
     * @return java.util.List<cc.zy.base.businesses.entity.College> 院校集合
     * @author LiPeng
     * @date 2021/3/9
     */
    @Override
    public IPage<Staff> findStaff(QueryRequest request, Staff staff) {

        Page<Staff> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countStaffDetail(staff));

        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        return baseMapper.findStaffDetailPage(page, staff);

    }

    /**
     * @Author: DYQ
     * @Description: 查询前台输入的全称或简称是否存在
     * @DateTime: 2022/3/9 11:30
     * @Params: java.lang.Boolean
     * @Return [name, alias]
     */
    @Override
    public Boolean isRepetitive(String name,String alias) {

        List<Staff> repetitive = staffMapper.isRepetitive(name, alias);

        Boolean flag = true;
        if (repetitive != null && repetitive.size() > 0) {
            flag = false;
        }

        return flag;

    }

    /**
     * @Author: WBR
     * @Description: 根据标签查询员工
     * @DateTime: 2022/3/11 17:03
     * @Params: [request, staff]
     * @Return com.baomidou.mybatisplus.core.metadata.IPage<cc.zy.base.hr.entity.Staff>
     */
    @Override
    public IPage<Staff> findStaffByLabel(QueryRequest request, Staff staff) {

        Page<Staff> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countStaffDetailByLabel(staff));

        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        return baseMapper.findStaffDetailPageByLel(page, staff);

    }

    /**
     * @Author: WBR
     * @Description: 查询标签id
     * @DateTime: 2022/3/11 17:03
     * @Params: []
     * @Return java.util.List<cc.zy.base.hr.entity.Staff>
     */
    @Override
    public List<Staff> findStaffForSelect() {
        return staffMapper.selectStaffForSelect();
    }

}
